20230322
[shlib.git] / doc / UM.txt.en_US / =2.1.shlibinc and basical usage.txt
blob33a96d87f2252844d89741489d81778f8f4db155
1 doc of shlib
3   Copyright (C) 2022- Free Software Foundation, Inc.
5   Copying and distribution of this file, with or without modification,
6   are permitted in any medium without royalty provided the copyright
7   notice and this notice are preserved.
9 The following contributions warranted legal paper exchanges with the
10 Free Software Foundation.
13 # catagory
14 ==========
15 1.introduction
16 2.usage of shlib
17 2.1.include path
18 2.2.uniq and nest include
19 2.3.shlib element
20 2.4.include and laod in sub-proc
21 2.5.load and unload and reload
22 2.6.debug info output
23 2.7.include/load operation for other lib files
24 3.writing a shlib
30 # 1.introduction
31 ================
32     shlibinc is the basical cmd in shlib. it provide some function and alias
33 cmd, to implement the feature of library include/load.
34     it is different from 'source' cmd in system, the feature listed below:
36 @ it uses a global dir to store the shlib file, and include/load them without 
37   path prefix. a set of function is used for get/set/del include path.
38 @ it support global dir shlib include, and also support relative path include, 
39   it is used as '#include' in c language.
40 @ include operation can be nested with each other. and the file has been 
41   included will not be sourced again, it saves cpu cost. this will help for
42   lots of shlib file nested including.
43 @ load operation is supported. normally, load usefull shlib in system init 
44   script, and the include operation in program will be compacted to execute
45   the variable init code only. this also saves cpu cost.
46 @ unload and reload is the relative feature with load cmd. when we debug a 
47   shlib, we want to update code in memory, reload is used for this. if some 
48   feature is not worked normally in load operation, and unload it, it will
49   be included in script every time.
50 @ this module support debug info output by setting of shlibopt. when option 
51   setted, some of the detail info for debug will output to stderr. it is used 
52   for debug a new shlib, espacially in shlib nest including.
56 # 2.usage of shlib
57 ================
58         in shell command programe, source is a command to 'include' other script
59 into current code. but it uses file without global path prefix. and it can not
60 be un-include in program.
61     but 'source' cmd is the base of shlibinc feature.
62         look at the code below, and it's a very simple use of shlib.
64 ```
65 # pay attension to the space between . and shlibinc.
66 . shlibinc
68 # this shlib is a general string output shlib with different kinds of output
69   info.
70 include stdio.shlib
72 dbgout "use the function dbgout() in dbgout.shlib, to output a string.\n"
73 ```
75     run this script, and you will get the string outputed by function dbgout, 
76 which is wroted in stdio.shlib.
80 # 2.1.include path
81 ==============
83     the difference between 'source' and 'include' is that, include has global
84 library file path.
85     the variable SHLIB_PATH is nearly equal to LD_LIBRARY_PATH. and shlibinc 
86 provide those functions to get/set/del lib path.
88 @ get_shlib_path, print shlib one path string by one line.
89 @ get_shlib_path_var <var-name>, get the shlib path in array var.
90 @ set_shlib_path <PATH>, set whole shlib path string.
91 @ add_shlib_path <PATH>, append path to shlib path.
92 @ del_shlib_path <PATH>, delete path by specified string.
94     the things to be pay attension for:
96 @ when same shlib file in different paths(include relative path) is included in 
97   script, the sequence of include is the sequence of path string in SHLIB_PATH,
98   and use relative path at last.
99 @ path prefix will be effect to uniq-include. a file in global path, can be include again by a different path prefix.
101     generally, we only use the file name for 'include' cmd without "./" or "~/"
102 prefix or absolute path, even if it can works.
106 # 2.2.uniq and nest include
107 =======================
109     script execute very slowly. the include operation is a code execution on
110 cpu, it is different from compile-time language like c/c++. if we include many
111 shlib, the include operation will cost cpu execution time, and the program will
112 delay for include shlib.
113     many public feature is included by other shlibs. in c/c++, we use #ifdef to
114 avoid header file re-include. we need the same feature in shlib.
116     when we include same shlib in the same process, include and load operation
117 will skip it by it self. it record shlib file in SHLIB_LIST by load cmd, in
118 SHLIB_INC_LIST by include cmd.
122 # 2.3.shlib element
123 ===============
125     a library is consisted by those resource:
127 @ variables, declare create var with -g and -x option. var with -x option can
128   be exported in sub-script-proc.
129     if the variable declared with -x option, the value can be exported to sub
130   process, whatever it is in include or load operation.
131 @ functions, the default function declare is not exported, it can not be used
132   in sub-script-proc.
133     functions loaded in shlib can be invoked in sub-proc by 'load' cmd only.
134 @ alias cmds, the default alias cmds is not exported, it's blank in a new sub
135   process.
136     alias loaded in shlib can be invoked in sub-proc by 'load' cmd only.
138     include or load a shlib, is used to access shlib element to implement some
139 features. os, include and load operation is orianted to those element.
143 # 2.4.include and laod in sub-proc
144 ===============================
146     another method to save cpu cost is 'load' cmd. 
147     it load shlib in system init script, or cmdline. when the shlib included in 
148 a script program, it will ignore whole source operation. the function has been 
149 loaded, the alias cmds will be loaded automatically, and it just invoke 
150 shlib_xxx_init() function to init variables.
152     shlib code effective scope:
154 @ same program, but different process. eg: ( cmd ), cmd will executed in a sub 
155   process. but it is forked from current script program, all global variable
156   and functions, and alias cmds can be use as the parent program process. but 
157   the modification of variables is not modified in parent process.
158 @ sub-script-process invoking. a normal script cmd invoke in a program, the
159   variable in sub-script-proc is kept only with -x option, the function only
160   can be invoked by declare -x setting, the alias cmds is blank in 
161   sub-script-proc.
163     shlibinc provide those alias cmds:
165 @ PROG_GVAR_INIT <shlib-name>, init global variable in shlib, when the lib file 
166   has been loaded.
167 @ GVAR_INIT <shlib-name>, uniq-include flag setting.
168 @ uniqlib <shlib-name>, if this lib is included or loaded, it will return from
169   source operation, to avoid loading followed shlib code.
171     cmd of 'load' is an operation between two process. 
173 @ when a shlib has been included in the same program, it ignore include shlib,
174   and the 'load' cmd is executed as normal.
175 @ when a shlib has been loaded in the same program, it ignore shlib include 
176   and load.
177 @ when parent program include a shlib, use include and load as normal.
178 @ when parent program load a shlib, include and load operation will be 
179   compacted, it just only invoke shlib_xxx_init() function to init variables 
180   in shlib.
182     use include and load, it will process those condition automatically.
183     the feature of load and uniq-include decrease code execute time for shlib 
184 using.
188 # 2.5.load and unload and reload
189 ============================
190     if we are writing a shlib, we need to debug it, and testing for load 
191 operation. if a shlib is loaded, it can not be load again by 'load' cmd.
192     use reload to update code for shlib. and use unload to delete it to
193 decrease memory cost.
194     they are used in cmdline as normal.
198 # 2.6.debug info output
199 ===================
200     debug info.
202 @ file include/load operation info.
203 @ var/func/alias in loaded file info.
206 # 2.7.include/load operation for other lib files
207 ============================================
208     include operation is a usefull feature to use a file as a lib file. it 
209 provide global include path, uniq-include, load compactly in sub process.
210     if we need another file type to be a library file, we use the defination
211 function poridev by shlibinc.
212         
213         imi file is a txt based config file, and i want to use it as a global
214 paramter storage file. this example shows how to define imi file as a library
215 file.
218 define_include imi loadimi imiload : "${ROOTFS}/usr/minc"
221     syntax: define_include <name> <inc-name> <loadcmd> <unloadcmd> <inc-path>
223 @ <name> : lib file ext name.
224 @ <inc-name> : include operation name for user.
225 @ <loadcmd> : the function or cmd invoked by actual file loading.
226 @ <unloadcmd> : the function or cmd invoked by actual file unloading.
227 @ <inc-path> : the path of default library path.
233 # 3.writing a shlib
234 =================
236 @ templete.
237 @ code style & code format.
238 @ comment.
239 @ shlib_xxx_init().
258     参考doc/example/templete-code/shlib_templete.shlib。
259 @ 定义shlib_shlib_init(),用于全局变量的定义和初始化,并在函数定义之后调用。当shlib第一次load时运行。
260 @ 在include之前调用uniqlib shlib,其下的代码对已load的shlib不运行,避免重复load。
261 @ 一些代码,像是全局变量初始化,在new-sub-process中需要重新初始化。uniqlib shlib之前调用PROG_GVAR_INIT shlib,调用shlib_shlib_init()之后,return。
262 @ 文件以comment-section划分为不同的code-section。代码编写时在对应的section中编码。例如public函数和private函数在不同的section存放,这样能较好地看到shlib库的接口函数。
263 @ public函数的comment信息比较详细,而private函数根据需要编写comment。
264 @ shlib的include是uniq的,只在首次include时调用shlib_shlib_init()。
266 # shlib的include只在当前进程有效,
267 # shlib在load时,函数可在sub-proc中有效。
268 # 无论是include还是load,var需要在sub-proc有效,需要使用declare -g -x定义。
269 # alias在load时,sub-proc不起作用。
270 # 使用load时,变量在当前proc进行使用,需要在sub-proc的include中重新init。
278 relative-path in nest inc
279 lib-path in nest inc
280 if a file is included, it may be based on relative-path or lib-path. 
283 @ inlude
284 # inc path get/set/del.
285 # inc shlib in lib-path.
286 # relative-path inc.
287 # relative-path in nest inc.
288 # inc priority between pwd  & lib path
290 @ sub-proc-inc
291 # nest-uniq-inc shlib in same process.
292 # include same shlib in sub-proc
294 @ load
295 # inc shlib in lib-path.
296 # relative-path inc.
297 # relative-path in nest inc.
298 # inc priority between pwd & lib path
300 # can't load after include
302 @ sub-proc-load
303 # nest-uniq-inc shlib in same process.
305 # include in same proc, skip include, load deny.
306 # load in same proc, skip include & load.
307 # include in parent-proc, include and load as normal.
308 # load in parent-proc, include and load by shlib_xxx_init() compactly.
310 @ debug-info
313 @ unload-reload
325 # @ three things to be payying attension, var, func, alias.
326 # @ several kinds of load enviroument:
327 # # 1.include at the first time.
328 # # 2.include first, and then include.
329 # # 3.include first, and then load.
330 # # 4.load at the first time.
331 # # 5.load first, and then include.
332 # # 6.load first, and then load.
333 # # 7.include at the parent process, include in sub-proc.
334 # # 8.include at the parent process, load in sub-proc.
335 # # 9.load at the parent process, include in sub-proc.
336 # # 10.load at the parent process, load in sub-proc.
337 # @ 
338 # # for 1 & 4, normal include/load operation. source whole shlib, 
339 #   but include operation does not set load-flag variable.
340 # # for 2 & 3 & 5 & 6, the second include/load operation is skipped.
341 # # for 3, running load again if there is no 'load' executed.
342 # # in the same process, once include or load, skip next time.
343 # # for 7 & 8, the first include operation is not effect on sub-proc,
344 #   it will be include/load as it was not included in parent process.
345 # # for 9 & 10, if load at the parent process, just invoke xxx_init()
346 #   to init the global variables, and skip the function loading when
347 #   it has been loaded.
349 # @ conclution:
350 # # in the same process, once include or load, skip next time.
351 # # include operation is effect only in current process.
352 # # if load shlib in the parent process, just only invoke xxx_init()
353 #   to init the global variables in sub-process.
357 # @ flags
358 # # SHLIB_INC_LIST is used to tag include operation.
359 # # SHLIB_LIST with -x attr is used to tag load operation in different process.
360 # # uniq include tag have no -x attr, and uniq load set with -x attr. once loaded,
361 #   just only invoke xxx_init() without whole file loading.
365 # @ content
366 # # func loaded by load cmd is exported function, but include cmd is not.
367 # # var declared by include/load cmd is normal var. setting with -x attr 
368 #   is decided by developer manually.
369 # # alias can not be loaded in sub-proc. save the alias defination in a var, 
370 #   re-define them when xxx_init() is invoked.
390 @ load命令调用时保存shlib的使用记录。系统定时任务中根据使用频率,以及shlib对内存资源的使用,设置初始化时load一些shlib,减少程序运行时shlib初始化的cpu使用。
394 [shlibinc]
395 @ 全局变量名称尽量添加shlib模块的pfx,减少共用name-space的使用。
397 @ 对程序库文件的操作类型,包括:
398 # source:最基础的操作,只对脚本库进行函数加载。
399 # include:对source功能的编程语言化操作。包括lib-path、uniq-inc功能。
400 # load:对sub-proc的shlib进行source包含,并使shlib在sub-proc中无须重复source。
401 # reload:shlib的load是uniq的,load之后不进行重复load。但对于shlib更新时,使用reload命令,将已有shlib unload之后,再重新load。
403 @ include 与 load
404 # include操作只在当前进程有效,通常用于代码中对shlib的使用。但对已load的shlib进行判断,从而skip库的include。
405 # load操作在sub-proc中仍可使用,通常用于系统初始化时对shlib的load,减少程序运行时include的cpu使用。
408 @ shlib的load操作
409 # 一个shlib功能模块,包含3个部分的定义:var、func、alias。考虑三者在不同进程load时的使用,以及在xxx_init()中的设置。
410 # first-load对shlib进行source包含,并对var、func、alias的名称或内容进行保存,对func添加-x属性,设置成export的函数。
411 # 在second-load时,将保存的alias定义,进行重新设置。
412 # 在second-load时,一些alias在xxx_init()中进行重新设置。
413 # 在unload时,unset、unalias保存的这些内容。
415 @ shlib中的var
416 # var划分为export属性和非export属性,两者区别在于是否在sub-proc中仍可使用。(是否设置export,根据shlib中的func对var的使用)
417 # 在xxx_init()中,对variable定义和初始化。export属性的变量使用decl_shlib_var定义,包含对变量existing的判断,变量的定义,变量初始化值的设置。
419 @ shlib中的func
420 # func在load时始终是export属性的。
423 @ shlib中的alias
424 # alias在new-proc时,始终是unexport的。
425 # alias在first-load时保存定义,在second-load时自动设置。
426 # 对于一些在定义中包含${}环境变量expanssion的alias,在second-load时调用的xxx_init()中重新定义。
430 # 对于lib在load时的初始化。不同进程load时,以及first load时,xxx_init()函数的运行。一些export属性的变量在初始化时,需考虑变量是否已定义,以及是否需要re-init。
431 例如:stdio.shlib中的进程id,在first-init时,初始化为当前pid的非export环境变量。在second-init时,变量未定义,重新定义。
432 例如:term.shlib中定义的一些表示颜色的常量,定义成export属性,init时无须重复初始化,减少cpu消耗。
433 例如:stdio.shlib中定义的调试信息输出的pfx和sfx,在同一个session下的style相同,所以在second-load时不进行重新设置,根据当前进程的设置使用。定义成export,
434 例如:
436 @ 使用cp命令时,不添加-v参数,-v参数使用的absolutly-path包含不同测试环境的路径信息。
437 @ shlib中的环境变量的定义,需要考虑reload,uniq-include等环境,变量的-g、-x属性,以及变量的名字空间,与local变量定义的问题,-a、-A属性无法export,等。